package com.hz.ruoyiseller.controller;

import java.sql.Timestamp;
import java.text.SimpleDateFormat;
import java.util.List;
import javax.servlet.http.HttpServletResponse;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.hz.rouyigonggong.domain.LgSeller;
import com.hz.ruoyiseller.service.LgSellerService;
import com.hz.ruoyiseller.queryentity.LgSellerQueryEntity;
import com.ruoyi.common.security.utils.SecurityUtils;
import com.ruoyi.system.api.model.LoginUser;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.ruoyi.common.log.annotation.Log;
import com.ruoyi.common.log.enums.BusinessType;
import com.ruoyi.common.security.annotation.RequiresPermissions;
import com.ruoyi.common.core.web.controller.BaseController;
import com.ruoyi.common.core.web.domain.AjaxResult;
import com.ruoyi.common.core.utils.poi.ExcelUtil;
import com.ruoyi.common.core.web.page.TableDataInfo;

/**
 * 卖家Controller
 * 
 * @author grx
 * @date 2025-03-06
 */
@RestController
@Slf4j
@RequestMapping("/people")
public class LgSellerController extends BaseController {
    @Autowired
    private LgSellerService lgSellerService;





    @PostMapping("/login")
    public AjaxResult loginSeller(String account, String password){
        QueryWrapper<LgSeller> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("account",account);
        queryWrapper.eq("password",password);
        LgSeller lgSeller = lgSellerService.getOne(queryWrapper);
        return AjaxResult.success("登录页面", lgSeller);
    }

    /**
     * 查询卖家列表
     */
    @RequiresPermissions("seller:people:list")
    @GetMapping("/list")
    public TableDataInfo list(LgSellerQueryEntity lgSellerQueryEntity) {
        startPage();
        QueryWrapper<LgSeller> queryWrapper = new QueryWrapper<>();
        List<String> createTimeList = lgSellerQueryEntity.getCreateTime();

// 判断 createTime 是否为 null，并确保有两个有效的日期
        if (createTimeList != null && createTimeList.size() == 2) {
            try {
                // 将日期字符串转换为 Timestamp 类型
                SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");  // 假设格式是 yyyy-MM-dd
                Timestamp startTime = new Timestamp(sdf.parse(createTimeList.get(0)).getTime());
                Timestamp endTime = new Timestamp(sdf.parse(createTimeList.get(1)).getTime());

                // 使用 QueryWrapper 进行 BETWEEN 查询
                queryWrapper.between("create_time", startTime, endTime);

            } catch (Exception e) {
                e.printStackTrace();
                // 处理解析异常
            }
        }

            queryWrapper.like(lgSellerQueryEntity.getAccount() != null, "account", lgSellerQueryEntity.getAccount());
            queryWrapper.like(lgSellerQueryEntity.getUsername() != null, "username", lgSellerQueryEntity.getUsername());
            queryWrapper.like(lgSellerQueryEntity.getRealName() != null, "real_name", lgSellerQueryEntity.getRealName());
            queryWrapper.eq(lgSellerQueryEntity.getGender() != null,"gender", lgSellerQueryEntity.getGender());
            queryWrapper.like(lgSellerQueryEntity.getAddress() != null, "address", lgSellerQueryEntity.getAddress());
            List<LgSeller> list = lgSellerService.list(queryWrapper);
            return getDataTable(list);
        }




    /**
     * 导出卖家列表
     */
    @RequiresPermissions("seller:people:export")
    @Log(title = "卖家", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, LgSeller lgSeller)
    {
        //List<LgSeller> list = lgSellerService.selectLgSellerList(lgSeller);
        List<LgSeller> list = lgSellerService.list(null);
        ExcelUtil<LgSeller> util = new ExcelUtil<LgSeller>(LgSeller.class);
        util.exportExcel(response, list, "卖家数据");
    }

    /**
     * 获取卖家详细信息
     */
    @RequiresPermissions("seller:people:query")
    @GetMapping(value = "/{sellerId}")
    public AjaxResult getInfo(@PathVariable("sellerId") String sellerId)
    {
        return success(lgSellerService.getById(sellerId));
    }

    /**
     * 新增卖家
     */
    @RequiresPermissions("seller:people:add")
    @Log(title = "卖家", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody LgSeller lgSeller)
    {

    return toAjax(lgSellerService.save(lgSeller));
    }

    /**
     * 修改卖家
     */
    @RequiresPermissions("seller:people:edit")
    @Log(title = "卖家", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody LgSeller lgSeller)
    {
//        return toAjax(lgSellerService.updateLgSeller(lgSeller));
        return toAjax(lgSellerService.updateById(lgSeller));
    }

    /**
     * 删除卖家
     */
    @RequiresPermissions("seller:people:remove")
    @Log(title = "卖家", businessType = BusinessType.DELETE)
	@DeleteMapping("/{sellerIds}")
    public AjaxResult remove(@PathVariable String[] sellerIds)
    {
        return toAjax(lgSellerService.deleteLgSellerBySellerIds(sellerIds));
    }
}
